<?php
/*
 * @Author: 莫莫
 * @Phone: 13450159681
 * @Description: 
 * @Date: 2024-08-19 14:26:38
 * @LastEditTime: 2025-05-06 16:02:01
 */

namespace app\common\library;

use think\cache\driver\Redis;

class Api
{
    protected $_url = 'http://api1.gdobee.top/api/v1';
    protected $account = 'ZHISHUISHANGYUAN'; //账号
    protected $password = 'ZHISHUISHANGYUAN123456'; //密码
    protected $syskey = 'ZHISHUISHANGYUAN';
    protected $_token = '';

    //登录
    public function login($account = null, $password = null)
    {
        $data = [
            'account' => $account ? $account : $this->account,
            'password' => $password ? $password : $this->password,
            'type' => 'WEB',
        ];
        $url = $this->_url . '/login/access-token';
        $res = $this->_request($url, json_encode($data),false,'POST',false);
        $redis = new Redis();
        // 设置缓存
        $redis->set('_token', $res['data']['token'], $res['data']['token_expire'] - 300);

        return $this->getToken();
    }
    public function statUserCount()
    {

        $url = $this->_url . '/statistics/zssy/stat-user-count';
        $res = $this->_request($url, null, false, 'GET');
        return $res;
    }

    public function getUserList()
    {
        $limit = 1000;
        $skip = 0;
        $url = $this->_url . '/users/zssy/get-user-list?limit=' . $limit. '&skip=' . $skip;
        $res = $this->_request($url, null, false, 'GET');
        return $res;
    }
    public function statUserOrder($userType,$userID=null, $limit = 10, $page = 1)
    {
        $limit = $limit ? $limit : 10;
        $skip = ($page - 1) * $limit;
        $data = [
            
        ];
        $url = $this->_url . '/statistics/zssy/stat-user-order?userType=' . $userType. '&limit=' . $limit. '&skip=' . $skip. '&agentID='. $userID;
        $res = $this->_request($url, json_encode($data), false, 'GET');
        return $res;
    }

    public function statOneUserOrder($userType,$userID=null, $limit = 10, $page = 1)
    {
        $limit = $limit ? $limit : 10;
        $skip = ($page - 1) * $limit;
        $data = [
            
        ];
        $url = $this->_url . '/statistics/zssy/stat-one-user-order?userType=' . $userType. '&limit=' . $limit. '&skip=' . $skip.'&userID='. $userID;
        $res = $this->_request($url, json_encode($data), false, 'GET');
        return $res;
    }
    //获取token
    public function getToken()
    {
        $redis = new Redis();
        // $redis->rm('_token');
        return $redis->get('_token');
    }
    //设置网络请求配置
    public function _request($curl, $data = null, $https = true, $method = 'POST', $is_login = true)
    {
        // 创建一个新cURL资源
        $ch = curl_init();
        if ($is_login) {
            $this->login();
        }
        $headers = array(
            'Content-Type: application/json',
            'syskey: ' . $this->syskey,
            'token:' . $this->getToken(),
        );
        // 设置URL和相应的选项
        curl_setopt($ch, CURLOPT_URL, $curl);    //要访问的网站
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_HEADER, false);    //启用时会将头文件的信息作为数据流输出。
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  //将curl_exec()获取的信息以字符串返回，而不是直接输出。 

        if ($https) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  //FALSE 禁止 cURL 验证对等证书（peer's certificate）。
            // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);  //验证主机
        }
        if ($method == 'POST') {
            curl_setopt($ch, CURLOPT_POST, true);  //发送 POST 请求
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  //全部数据使用HTTP协议中的 "POST" 操作来发送。
        }
        // 抓取URL并把它传递给浏览器
        $content = curl_exec($ch);
        if ($content  === false) {
            return "网络请求出错: " . curl_error($ch);
            exit();
        }
        //关闭cURL资源，并且释放系统资源
        curl_close($ch);
        $res = json_decode($content, true);
        if ($res['code'] == 424 || $res['code'] == 4003) {
            // $this->login();
            $this->_request($curl, $data, $https, $method);
        }
        // if ($res['code'] != 200) {
        //     return $res['message'];
        // }
        return $res;
    }
}
